//加载字典
function dictJudge(value, options, row) {
    //sex为自定义字典项
    var statusObj = vm.options.sex;
    for (var i = 0; i < statusObj.length; i++) {
        if (statusObj[i].value == value) {
            return '<span>' + statusObj[i].text + '</span>';
        }
    }
}
$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'onlinestudy/question/list',
        datatype: "json",
        colModel: [
            {label: 'id', name: 'id', index: 'id', width: 50, key: true, hidden: true},
            {label: '题目说明', name: 'title', index: 'title', width: 160},
            {
                label: '题目类型',
                name: 'questionType',
                index: 'question_type',
                width: 80,
                formatter: (value, options, rowObj) => {
                    if (value == "0") {
                        return "单选";
                    } else if (value == "1") {
                        return "判断";
                    } else if (value == "2") {
                        return "多选";
                    } else {
                        return value;
                    }
                }
            },
            {label: '图片或视频内容', name: 'image', index: 'image', width: 80,hidden:true},
            {label: '答案说明', name: 'description', index: 'description', width: 80,hidden:true},
            {label: '答案A', name: 'answerA', index: 'answer_a', width: 80,hidden:true},
            {label: '答案B', name: 'answerB', index: 'answer_b', width: 80,hidden:true},
            {label: '答案C', name: 'answerC', index: 'answer_c', width: 80,hidden:true},
            {label: '答案D', name: 'answerD', index: 'answer_d', width: 80,hidden:true},
            {label: 'A是否正确答案', name: 'isAnswerA', index: 'is_answer_a', width: 80,hidden:true},
            {label: 'B是否正确答案', name: 'isAnswerB', index: 'is_answer_b', width: 80,hidden:true},
            {label: 'C是否正确答案', name: 'isAnswerC', index: 'is_answer_c', width: 80,hidden:true},
            {label: 'D是否正确答案', name: 'isAnswerD', index: 'is_answer_d', width: 80,hidden:true},
            {label: '创建时间', name: 'createTime', index: 'create_time', width: 80,formatter:formatSubscribeTime},
            {label: '创建人', name: 'createName', index: 'create_name', width: 80,hidden:true},
            {label: '', name: 'state', index: 'state', width: 80,hidden:true}
        ],
        viewrecords: true,
        height: 385,
        rowNum: 10,
        rowList: [10, 30, 50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth: true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader: {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames: {
            page: "page",
            rows: "limit",
            order: "order"
        },
        gridComplete: function () {
            //隐藏grid底部滚动条
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({"overflow-x": "hidden"});
        }
    });
});

var vm = new Vue({
    el: '#rrapp',
    data: {
        showList: true,
        title: "title",
        question:{}
    },
    created:function(){
    },
    methods: {
        query: function () {
            vm.reload();
        },
        add: function () {
            vm.showList = false;
            vm.title = "新增";
            vm.question = {questionType: '',title: ''};
        },
        update: function (event) {
            var id = getSelectedRow();
            if (id == null) {
                return;
            }
            vm.showList = false;
            vm.title = "修改";

            vm.getInfo(id)
        },
        saveOrUpdate: function (event) {
            var formdata = new FormData(document.getElementById("questionForm"));
            var url = vm.question.id == null ? "onlinestudy/question/save" : "onlinestudy/question/update";
            $.ajax({
                type: "POST",
                url: baseURL + url,
                contentType: "application/json",
                data: formdata,
                processData:false,
                contentType:false,
                success: function (r) {
                    if (r.code === 0) {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    } else {
                        alert(r.msg);
                    }
                }
            });
        },
        del: function (event) {
            var ids = getSelectedRows();
            if (ids == null) {
                return;
            }

            confirm('确定要删除选中的记录？', function () {
                $.ajax({
                    type: "POST",
                    url: baseURL + "onlinestudy/question/delete",
                    contentType: "application/json",
                    data: JSON.stringify(ids),
                    success: function (r) {
                        if (r.code == 0) {
                            alert('操作成功', function (index) {
                                $("#jqGrid").trigger("reloadGrid");
                            });
                        } else {
                            alert(r.msg);
                        }
                    }
                });
            });
        },
        getInfo: function (id) {
            $.get(baseURL + "onlinestudy/question/info/" + id, function (r) {
                vm.question = r.question;
            });
        },
        reload: function (event) {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                page: page
            }).trigger("reloadGrid");
        }
    }
});